home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / 3Dmodeling / liss.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  7.6 KB  |  344 lines

  1. /*
  2.  * Copyright 1992, 1993, 1994, Silicon Graphics, Inc.
  3.  * All Rights Reserved.
  4.  *
  5.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  6.  * the contents of this file may not be disclosed to third parties, copied or
  7.  * duplicated in any form, in whole or in part, without the prior written
  8.  * permission of Silicon Graphics, Inc.
  9.  *
  10.  * RESTRICTED RIGHTS LEGEND:
  11.  * Use, duplication or disclosure by the Government is subject to restrictions
  12.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  13.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  14.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  15.  * rights reserved under the Copyright Laws of the United States.
  16.  */
  17.  
  18. /* Tom Davis -- 1992 */
  19.  
  20. /* lissajous figures */
  21.  
  22. #include <stdio.h>
  23. #include <math.h>
  24. #include <gl.h>
  25. #include <device.h>
  26. #include "3d.h"
  27.  
  28.  
  29. long th=4;
  30. long ph=5;
  31. float theta, phi;
  32.  
  33. long STEPS;
  34. #define PI 3.14159265358
  35.  
  36. long lcm = 60;
  37.  
  38. long gcd(x, y)
  39. {
  40.     if (x % y == 0) return y;
  41.     return gcd( y, x % y);
  42. }
  43.  
  44. void errf(char *s)
  45. {
  46.     /*fprintf(stderr, "%s\n", s);*/
  47.     return;
  48. }
  49.  
  50. float x(float t)
  51. {
  52.     if (t == 1.0) t = 0.0;  /* guarantees wrap */
  53.     return sin(theta*t)*cos(phi*t);
  54. }
  55.  
  56. float xx(float t)
  57. {
  58.     if (t == 1.0) t = 0.0;  /* guarantees wrap */
  59.     return theta*cos(theta*t)*cos(phi*t)-phi*sin(phi*t)*sin(theta*t);
  60. }
  61.  
  62. float xxx(float t)
  63. {
  64.     if (t == 1.0) t = 0.0;  /* guarantees wrap */
  65.     return -(theta*theta+phi*phi)*sin(theta*t)*cos(phi*t)
  66.            -2*phi*theta*sin(phi*t)*cos(theta*t);
  67. }
  68.  
  69. float y(float t)
  70. {
  71.     if (t == 1.0) t = 0.0;  /* guarantees wrap */
  72.     return cos(theta*t);
  73. }
  74.  
  75. float yy(float t)
  76. {
  77.     if (t == 1.0) t = 0.0;  /* guarantees wrap */
  78.     return -theta*sin(theta*t);
  79. }
  80.  
  81. float yyy(float t)
  82. {
  83.     if (t == 1.0) t = 0.0;  /* guarantees wrap */
  84.     return -theta*theta*cos(theta*t);
  85. }
  86.  
  87. float z(float t)
  88. {
  89.     if (t == 1.0) t = 0.0;  /* guarantees wrap */
  90.     return sin(theta*t)*sin(phi*t);
  91. }
  92.  
  93. float zz(float t)
  94. {
  95.     if (t == 1.0) t = 0.0;  /* guarantees wrap */
  96.     return theta*cos(theta*t)*sin(phi*t) + phi*sin(theta*t)*cos(phi*t);
  97. }
  98.  
  99. float zzz(float t)
  100. {
  101.     if (t == 1.0) t = 0.0;  /* guarantees wrap */
  102.     return -(theta*theta+phi*phi)*sin(theta*t)*sin(phi*t)
  103.            +theta*phi*cos(theta*t)*cos(phi*t);
  104. }
  105.  
  106. float    blackvec[3] = {0.0, 0.0, 0.0};
  107. float    whitevec[3] = {1.0, 1.0, 1.0};
  108. static float idmat[4][4] = {1.0,0.0,0.0,0.0,
  109.                 0.0,1.0,0.0,0.0,
  110.         0.0,0.0,1.0,0.0,
  111.         0.0,0.0,0.0,1.0};
  112.  
  113. float  shiny_material[] = {SPECULAR, 0.8, 0.8, 0.8,
  114.                  DIFFUSE,  0.4, 0.4, 0.4,
  115.                      SHININESS, 30.0,
  116.                  LMNULL};
  117.  
  118. float  blue_light[] = {LCOLOR, 0.0, 0.0, 0.6,
  119.                POSITION, 0.0, 1.0, 0.0, 0.0,
  120.                LMNULL};
  121.  
  122. /*
  123. ** Tell the Graphics Library to DEFINE a simple lighting model
  124. ** that accounts for diffuse and ambient reflection.  This simple
  125. ** lighting model happens to be the default lighting model for 
  126. ** the Graphics Library.
  127. */
  128. void def_simple_light_model()
  129.  
  130. {
  131.     lmdef(DEFLMODEL, 1, 0, NULL);
  132.     lmdef(DEFMATERIAL, 1, 11, shiny_material);
  133.     lmdef(DEFLIGHT, 1, 0, NULL);
  134.     lmdef(DEFLIGHT, 2, 10, blue_light);
  135. }
  136.  
  137. /*
  138. ** Tell the Graphics Library to USE the simple lighting model
  139. ** that we defined earlier.
  140. */
  141. use_simple_light_model()
  142.  
  143. {
  144.     lmbind(LMODEL, 1);
  145.     lmbind(LIGHT0, 1);
  146.     lmbind(LIGHT1, 2);
  147. }
  148.  
  149. /* Here's an idiotic display list.  Just save the vectors and
  150.  * normals in a giant list, and spew them out in order when the
  151.  * draw command comes.
  152.  */
  153.  
  154. float vectlist[200000][3];
  155. float normlist[200000][3];
  156. long vcount;
  157.  
  158. float trilist[100000][3];
  159. float trinorm[100000][3];
  160. long tcount;
  161.  
  162. void initdisplaylist()
  163. {
  164.     tcount = vcount = 0;
  165. }
  166.  
  167. /* ... and the corresponding idiotic savefunc */
  168.  
  169. void savefunc(type, n0, v0, n1, v1, n2, v2, n3, v3)
  170. long    type;
  171. float    *n0, *n1, *n2, *n3, *v0, *v1, *v2, *v3;
  172. {
  173.     if (type == ADD_QUAD) {
  174.     if (vcount > 200000) {
  175.         fprintf(stderr, "too many points\n");
  176.         exit();
  177.     }
  178.     copy3(n0, &normlist[vcount][0]);
  179.     copy3(v0, &vectlist[vcount++][0]);
  180.     copy3(n1, &normlist[vcount][0]);
  181.     copy3(v1, &vectlist[vcount++][0]);
  182.     copy3(n2, &normlist[vcount][0]);
  183.     copy3(v2, &vectlist[vcount++][0]);
  184.     copy3(n3, &normlist[vcount][0]);
  185.     copy3(v3, &vectlist[vcount++][0]);
  186.     } else {
  187.     if (tcount > 100000) {
  188.         fprintf(stderr, "too many points\n");
  189.         exit();
  190.     }
  191.     copy3(n0, &trinorm[tcount][0]);
  192.     copy3(v0, &trilist[tcount++][0]);
  193.     copy3(n1, &trinorm[tcount][0]);
  194.     copy3(v1, &trilist[tcount++][0]);
  195.     copy3(n2, &trinorm[tcount][0]);
  196.     copy3(v2, &trilist[tcount++][0]);
  197.     }
  198. }
  199.  
  200. void drawdisplaylist()
  201. {
  202.     long i;
  203.  
  204.     for (i = 0; i < vcount;) {
  205.     bgnpolygon();
  206.     n3f(&normlist[i][0]);
  207.     v3f(&vectlist[i++][0]);
  208.     n3f(&normlist[i][0]);
  209.     v3f(&vectlist[i++][0]);
  210.     n3f(&normlist[i][0]);
  211.     v3f(&vectlist[i++][0]);
  212.     n3f(&normlist[i][0]);
  213.     v3f(&vectlist[i++][0]);
  214.     endpolygon();
  215.     }
  216.     for (i = 0; i < tcount;) {
  217.     bgnpolygon();
  218.     n3f(&trinorm[i][0]);
  219.     v3f(&trilist[i++][0]);
  220.     n3f(&trinorm[i][0]);
  221.     v3f(&trilist[i++][0]);
  222.     n3f(&trinorm[i][0]);
  223.     v3f(&trilist[i++][0]);
  224.     endpolygon();
  225.     }
  226. }
  227.  
  228. void drawworld()
  229. {
  230.     c3f(blackvec);
  231.     clear();
  232.     zclear();
  233.     drawdisplaylist();
  234. }
  235.  
  236.  
  237. static long menu, materials, models;
  238.  
  239. initmenus()
  240. {
  241.     materials = defpup("Shiny%x1|Purple%x2|Chalk%x3|Blue%x4|Red Plastic%x5|Cyan%x6");
  242.     models = defpup("Ethanol%x100|SGI Logo%x101|Benzene%x102|Spiral%x103");
  243.     addtopup(models, "Solid of Revolution%x107");
  244.     addtopup(models, "Smooth Solid of Rev.%x104|Square Worm%x105|Platonic Solids%x106");
  245.     addtopup(models, "Warped Stuff%x108");
  246.     menu = defpup("Models %m|Materials %m|Save Spin%x999|Quit%x1000", models, materials);
  247. }
  248.  
  249. static void openfile(long);
  250. FILE *binfile;
  251.  
  252. void savetospinfile()
  253. {
  254.     long i;
  255.  
  256.     openfile(vcount + (tcount*4)/3);
  257.     for (i = 0; i < vcount; i++) {
  258.     fwrite(&normlist[i][0], 4, 3, binfile);
  259.     fwrite(&vectlist[i][0], 4, 3, binfile);
  260.     }
  261.     for (i = 0; i < tcount;) { /* hack!!!! write triangle as quad */
  262.     fwrite(&trinorm[i][0], 4, 3, binfile);
  263.     fwrite(&trilist[i++][0], 4, 3, binfile);
  264.     fwrite(&trinorm[i][0], 4, 3, binfile);
  265.     fwrite(&trilist[i++][0], 4, 3, binfile);
  266.     fwrite(&trinorm[i][0], 4, 3, binfile);
  267.     fwrite(&trilist[i][0], 4, 3, binfile);
  268.     fwrite(&trinorm[i][0], 4, 3, binfile);
  269.     fwrite(&trilist[i++][0], 4, 3, binfile);
  270.     }
  271.     fclose(binfile);
  272.     return;
  273. }
  274.  
  275. void printshit()
  276. {
  277.     long i;
  278.  
  279.     for (i = 0; i < vcount; i++) {
  280.         printf("%g, %g, %g\n",
  281.         vectlist[i][0], vectlist[i][1], vectlist[i][2]);
  282.         printf("%8.8x, %8.8x, %8.8x\n\n",
  283.         *(long *)&vectlist[i][0], *(long *)&vectlist[i][1], *(long *)&vectlist[i][2]);
  284.     }
  285. }
  286.     
  287. static void openfile(long pcount)
  288. {
  289.     long magic[3];
  290.  
  291.     magic[0] = 0x5423;
  292.     magic[1] = pcount*4;
  293.     magic[2] = 0;   /* no color values */
  294.  
  295.     if ((binfile = fopen("model.spin", "w")) == 0) {
  296.     fprintf(stderr, "couldn't open model.spin\n");
  297.     return;
  298.     }
  299.     fwrite(magic, 4, 3, binfile);
  300. }
  301.                             
  302.  
  303.  
  304.  
  305. main(int argc, char **argv)
  306. {
  307.     long i;
  308.     curve_t *lisscrv;
  309.  
  310.     if (argc != 3) {
  311.     printf("usage: %s n1, n2\n", argv[0]);
  312.     exit(-1);
  313.     }
  314.     seterrorfunc(errf);
  315.     th = atoi(argv[1]);
  316.     ph = atoi(argv[2]);
  317.     lcm = th*ph/gcd(th, ph);
  318.     theta = th*2.0*PI;
  319.     phi = ph*2.0*PI;
  320.     STEPS = 500/lcm;
  321.     keepaspect(1, 1);
  322.     foreground();
  323.     winopen("lissajous");
  324.     backface(1);
  325.     RGBmode();
  326.     doublebuffer();
  327.     zbuffer(TRUE);
  328.     gconfig();
  329.     mmode(MVIEWING);
  330.     perspective(450, 1.0, 1.0, 15.0);
  331.     def_simple_light_model();
  332.     use_simple_light_model();
  333.     lisscrv = newanalyticcurve(x, y, z, xx, yy, zz, xxx, yyy, zzz);
  334.     makeworm(lisscrv, .11, 12, 350, savefunc);
  335.     lmbind(MATERIAL, 1);
  336.     /*
  337.     savetospinfile();
  338.     */
  339.     /*printshit();*/
  340.     while (1) {
  341.     trackmodel(drawworld, 4.0, 0.0, 0.0, 0.0);
  342.     }
  343. }
  344.